package com.atguigui.leetcode1;

/**
 * P面试题0502BianryNumberToStringLcci
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/3/2 8:53
 */
public class P面试题0502BianryNumberToStringLcci {
    public static void main(String[] args) {
        Solution solution = new P面试题0502BianryNumberToStringLcci().new Solution();
        // TO TEST
    }

    class Solution {
        public String printBin(double num) {
            StringBuilder sb = new StringBuilder("0.");
            //每次将实数乘以 222，将此时的整数部分添加到二进制表示的末尾，然后将整数部分置为 000，重复上述操作，直到小数部分变成 000 或者小数部分出现循环时结束操作
            while (sb.length() <= 32 && num != 0) {
                num *= 2;
                int digit = (int) num;
                sb.append(digit);
                num -= digit;
            }

            return sb.length() <= 32 ? sb.toString() : "ERROR";
        }
    }
}
